/**
 * 格式化数据，背景色，背景图片链接，文字，边框等
 * @param {Object} data 数据源
 */
let formatCanvasData = (data) => {
  // 要绘制的节点，及默认背景色
  let result = {
    selector: '#drawImgCanvas', // canvas的selector
    drawAreaSelector: '.center-shadow .cont', // 绘制区域的selector
    autoSize: true,
    scale: data.scale || 1,
    data: {
      backgroundColor: 'transparent',
      node: []
    }
  }
  // 图片白色背景
  result.data.node.push({
    css: {
      backgroundColor: '#ffffff',
      borderRadius: '10px'
    }
  })
  // 商家logo
  result.data.node.push({
    src: data.imgDomain + data.logo,
    mode: 'aspectFit'
  })
  // 促销标题
  let promoteInfo = data.promoteInfo || {}
  result.data.node.push({
    text: promoteInfo.name || '',
    css: {
      textAlign: 'center',
      color: '#333',
      fontSize: '28rpx'
    }
  })
  // 促销类型
  if (promoteInfo.detail) {
    // 促销类型背景色
    result.data.node.push({
      css: {
        borderRadius: '25rpx',
        backgroundColor: '#ff5858'
      }
    })
    // 促销类型文字
    result.data.node.push({
      text: promoteInfo.detail || '',
      css: {
        textAlign: 'center',
        color: '#fff',
        fontSize: '28rpx',
      }
    })
  } else {
    result.data.node.push({
      text: '',
      css: {}
    })
    result.data.node.push({
      text: '',
      css: {}
    })
  }
  // 循环遍历促销详情
  let promotLadder = promoteInfo.promotLadder || []
  for (let i in promotLadder) {
    let promotLadderItem = promotLadder[i].list || []
    for (let j in promotLadderItem) {
      result.data.node.push({
        text: promotLadderItem[j].name,
        css: {
          textAlign: 'center',
          color: '#333',
          fontSize: '28rpx'
        }
      })
    }
  }
  let prom = promoteInfo.prom || {}
  // 必买商品
  if (prom.MustBuyGoods.length > 0) {
    result.data.node.push({
      text: `必买商品${prom.MustBuyGoods.length}种`,
      css: {
        textAlign: 'center',
        color: '#333',
        fontSize: '28rpx'
      }
    })
  }
  //促销时间范围
  result.data.node.push({
    text: prom.StartTime + " - " + prom.EndTime,
    css: {
      textAlign: 'center',
      color: '#333',
      fontSize: '28rpx'
    }
  })
  // 小程序码/二维码
  result.data.node.push({
    src: data.qrCode,
    mode: 'aspectFit'
  })
  result.data.node.push({
    text: '长按/扫码识别',
    css: {
      color: '#999999',
      fontSize: '28rpx'
    }
  })
  return result
}

export {
  formatCanvasData
}